//
//  CustomTabBarCtrl.m
//

#import "CustomTabBarCtrl.h"
#import "HomeCtrl.h"
#import "ProjectCtrl.h"
#import "LifeCtrl.h"
#import "LWMeCtrl.h"

@interface CustomTabBarCtrl ()<UITabBarControllerDelegate>

@property (nonatomic, weak) HomeCtrl *home;
@property (nonatomic, weak) ProjectCtrl *project;
@property (nonatomic, weak) LifeCtrl *life;
@property (nonatomic, weak) LWMeCtrl *me;

@property (nonatomic, weak) UIViewController *lastSelectedViewContoller;

@end

@implementation CustomTabBarCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDelegate:self];
    [self.tabBar setBackgroundImage:IMG(@"tabbar_background")];
    
    HomeCtrl *home = [[HomeCtrl alloc]init];
    [self addOneChlildVc:home title:@"主页" imageName:@"lw_home" selectedImageName:@"lw_home_selected"];
    self.home = home;
    
    ProjectCtrl *project = [[ProjectCtrl alloc] init];
    [self addOneChlildVc:project title:@"主程信息" imageName:@"lw_project" selectedImageName:@"lw_project_selected"];
    self.project = project;
    
    LifeCtrl *life = [[LifeCtrl alloc] init];
    [self addOneChlildVc:life title:@"生活百科" imageName:@"lw_life" selectedImageName:@"lw_life_selected"];
    self.life = life;
    
    LWMeCtrl *me = [[LWMeCtrl alloc] init];
    [self addOneChlildVc:me title:@"个人中心" imageName:@"lw_me" selectedImageName:@"lw_me_selected"];
    self.me = me;
    
    
    [[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(toHomeCtrl)name:@"toHomeCtrl" object:nil];
    
}

-(void)toHomeCtrl{
    [self setSelectedClass:[HomeCtrl class]];
    [self.viewControllers makeObjectsPerformSelector:@selector(popToRootViewControllerAnimated:) withObject:[NSNumber numberWithBool:YES]];
}

-(void)setSelectedClass:(Class)class{
    for (UINavigationController *ctrl in self.viewControllers) {
        if ([[ctrl.viewControllers firstObject] isKindOfClass:class]) {
            NSUInteger index = [self.viewControllers indexOfObject:ctrl];
            [self setSelectedIndex:index];
            break;
        }
    }
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController{
    UIViewController *vc = [viewController.viewControllers firstObject];
    self.lastSelectedViewContoller = vc;
}



/**
 *  添加一个子控制器
 *
 *  @param childVc           子控制器对象
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)addOneChlildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 设置标题
    // 相当于同时设置了tabBarItem.title和navigationItem.title
//    childVc.title = title;
    
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    // 声明这张图片用原图(别渲染)
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = selectedImage;
    childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    [childVc.tabBarItem setSelectedImage:selectedImage];
    // 设置tabBarItem的普通文字颜色
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
//    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置tabBarItem的选中文字颜色
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    //此主题颜色需要修改TODO
    selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
    [childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
    
    // 添加为tabbar控制器的子控制器
    UINavigationController *nav = [[CustomNavigationCtrl alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"toHomeCtrl" object:nil];
}

@end
